package view;

import java.awt.BorderLayout;
import java.awt.SystemColor;
import java.util.Arrays;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

import controller.BuyController;

public class PanelMua extends JPanel {
	private static final long serialVersionUID = -2688533316758084774L;
	private JTable table;
	private Vector<String> columnNames;
	private Vector<Vector<String>> data;
	private DefaultTableModel tableModel;
	private BuyController control;
	private JTextField sumMoney;
	private JTextField sumThue;
	private JTextField sum;
	private JTextField textField_5;

	public PanelMua() {
		this.control = new BuyController(this);
		setLayout(new BorderLayout(0, 0));

		JSplitPane splitPane = new JSplitPane();
		this.add(splitPane, BorderLayout.CENTER);
		splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
		splitPane.setOneTouchExpandable(true);
		splitPane.setResizeWeight(0.1);

		JPanel infor = new JPanel();
		splitPane.setLeftComponent(infor);
		infor.setLayout(new BorderLayout(0, 0));

		JPanel detail = new JPanel();
		infor.add(detail, BorderLayout.NORTH);
		detail.setBorder(new TitledBorder(UIManager
				.getBorder("TitledBorder.border"),
				"Chi ti\u1EBFt h\u00F3a \u0111\u01A1n", TitledBorder.LEADING,
				TitledBorder.TOP, null, SystemColor.textHighlight));
		detail.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.BUTTON_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.BUTTON_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.BUTTON_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.BUTTON_COLSPEC,},
			new RowSpec[] {
				FormFactory.DEFAULT_ROWSPEC,}));

		JLabel lblMH = new JLabel("Mã HĐ");
		detail.add(lblMH, "1, 1, right, default");

		textField_5 = new JTextField();
		detail.add(textField_5, "3, 1, fill, default");
		textField_5.setColumns(18);

		JLabel lblNgyLp = new JLabel("Ngày lập");
		detail.add(lblNgyLp, "5, 1, right, default");

		JComboBox<String> comboBox_2 = new JComboBox<String>();
		detail.add(comboBox_2, "7, 1, fill, default");

		JComboBox<String> comboBox_1 = new JComboBox<String>();
		detail.add(comboBox_1, "9, 1, fill, default");

		JComboBox<String> comboBox = new JComboBox<String>();
		detail.add(comboBox, "11, 1, fill, default");

		JPanel information = new JPanel();
		infor.add(information, BorderLayout.CENTER);
		information.setBorder(new TitledBorder(UIManager
				.getBorder("TitledBorder.border"),
				"Th\u00F4ng tin nh\u00E0 ph\u00E2n ph\u1ED1i",
				TitledBorder.LEADING, TitledBorder.TOP, null,
				SystemColor.textHighlight));
		information.setLayout(new BorderLayout(0, 7));

		JPanel phanPhoi = new JPanel();
		information.add(phanPhoi, BorderLayout.NORTH);
		phanPhoi.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.DEFAULT_COLSPEC, FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"), }, new RowSpec[] {
				FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC, }));

		JLabel lblName = new JLabel("Tên NPP");
		phanPhoi.add(lblName, "1, 1, left, default");

		JTextField textField_3 = new JTextField();
		phanPhoi.add(textField_3, "3, 1, 5, 1, fill, default");
		textField_3.setColumns(10);

		JLabel lblaCh = new JLabel("Địa chỉ");
		phanPhoi.add(lblaCh, "1, 3, left, default");

		JTextField textField_2 = new JTextField();
		phanPhoi.add(textField_2, "3, 3, 5, 1, fill, default");
		textField_2.setColumns(10);

		JLabel lblinThoi = new JLabel("Điện thoại");
		lblMH.setPreferredSize(lblinThoi.getPreferredSize());
		phanPhoi.add(lblinThoi, "1, 5, left, default");

		JTextField textField_1 = new JTextField();
		phanPhoi.add(textField_1, "3, 5, fill, default");
		textField_1.setColumns(10);

		JLabel lblFax = new JLabel("Fax");
		phanPhoi.add(lblFax, "5, 5, right, default");

		JTextField textField = new JTextField();
		phanPhoi.add(textField, "7, 5, fill, default");
		textField.setColumns(10);

		JLabel lblEmail = new JLabel("Email");
		phanPhoi.add(lblEmail, "1, 7, left, default");

		JTextField textField_4 = new JTextField();
		phanPhoi.add(textField_4, "3, 7, 5, 1, fill, default");
		textField_4.setColumns(10);

		JScrollPane note = new JScrollPane();
		information.add(note);
		note.setViewportBorder(new TitledBorder(UIManager
				.getBorder("TitledBorder.border"), "Ghi ch\u00FA",
				TitledBorder.LEADING, TitledBorder.TOP, null,
				SystemColor.activeCaption));

		JTextArea textArea = new JTextArea();
		textArea.setRows(2);
		note.setViewportView(textArea);

		JPanel produc = new JPanel();
		splitPane.setRightComponent(produc);
		produc.setBorder(new TitledBorder(UIManager
				.getBorder("TitledBorder.border"), "S\u1EA3n ph\u1EA9m",
				TitledBorder.LEADING, TitledBorder.TOP, null,
				SystemColor.textHighlight));
		produc.setLayout(new BorderLayout(0, 7));

		JScrollPane list = new JScrollPane();
		list.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
		list.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		produc.add(list, BorderLayout.CENTER);

		// TODO table
		table = new JTable();
		table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		table.setFillsViewportHeight(true);
		columnNames = new Vector<String>(Arrays.asList("M\u00E3 h\u00E0ng",
				"Lo\u1EA1i h\u00E0ng", "T\u00EAn h\u00E0ng",
				"\u0110\u01A1n v\u1ECB t\u00EDnh", "S\u1ED1 l\u01B0\u1EE3ng",
				"\u0110\u01A1n gi\u00E1", "Thu\u1EBF su\u1EA5t"));
		data = new Vector<Vector<String>>();
		tableModel = new DefaultTableModel(data, columnNames);
		table.setModel(tableModel);
		table.getTableHeader().setReorderingAllowed(false);
		table.setAutoCreateRowSorter(true);
		// table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
		list.setViewportView(table);

		// TODO tool bar
		JToolBar toolBar = new JToolBar();
		toolBar.setOrientation(SwingConstants.VERTICAL);
		toolBar.setFloatable(false);
		produc.add(toolBar, BorderLayout.EAST);

		JButton add = new JButton("");
		add.setActionCommand("add");
		add.addActionListener(control);
		toolBar.add(add);
		add.setFocusable(false);
		add.setIcon(new ImageIcon(getClass().getResource("/images/new.png")));
		add.setToolTipText("Thêm");

		JButton refresh = new JButton("");
		refresh.setActionCommand("ref");
		refresh.addActionListener(control);
		refresh.setFocusable(false);
		toolBar.add(refresh);
		refresh.setIcon(new ImageIcon(getClass().getResource(
				"/images/refresh.png")));
		refresh.setToolTipText("Refresh");

		JButton delete = new JButton("");
		delete.setActionCommand("del");
		delete.addActionListener(control);
		delete.setFocusable(false);
		toolBar.add(delete);
		delete.setIcon(new ImageIcon(getClass().getResource(
				"/images/delete.png")));
		delete.setToolTipText("Xóa");

		JButton comp = new JButton("");
		comp.setActionCommand("comp");
		comp.addActionListener(control);
		comp.setToolTipText("Tính tiền");
		comp.setFocusable(false);
		comp.setIcon(new ImageIcon(PanelMua.class
				.getResource("/images/dollar.png")));
		toolBar.add(comp);

		JButton button = new JButton("");
		button.setActionCommand("save");
		button.addActionListener(control);
		button.setFocusable(false);
		button.setIcon(new ImageIcon(PanelMua.class
				.getResource("/images/save.png")));
		button.setToolTipText("Lưu");
		toolBar.add(button);

		// TODO function
		JPanel thanhToan = new JPanel();
		this.add(thanhToan, BorderLayout.SOUTH);
		thanhToan.setBorder(new TitledBorder(UIManager
				.getBorder("TitledBorder.border"), "Thanh to\u00E1n",
				TitledBorder.LEADING, TitledBorder.TOP, null,
				SystemColor.textHighlight));
		thanhToan.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.DEFAULT_COLSPEC, FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"), }, new RowSpec[] {
				FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC, }));

		JLabel lblTngThnhTin = new JLabel("Tổng thành tiền");
		thanhToan.add(lblTngThnhTin, "1, 1, left, default");

		sumMoney = new JTextField();
		thanhToan.add(sumMoney, "3, 1, fill, default");
		sumMoney.setColumns(10);

		JLabel lblTngThu = new JLabel("Tổng tiền thuế");
		thanhToan.add(lblTngThu, "5, 1, left, default");

		sumThue = new JTextField();
		thanhToan.add(sumThue, "7, 1, fill, default");
		sumThue.setColumns(10);

		JLabel lblTinThanhTon = new JLabel("Tiền thanh toán");
		thanhToan.add(lblTinThanhTon, "1, 3, left, default");

		sum = new JTextField();
		thanhToan.add(sum, "3, 3, fill, default");
		sum.setColumns(10);

		JLabel lblHnhThcThanh = new JLabel("Hình thức thanh toán");
		thanhToan.add(lblHnhThcThanh, "5, 3, left, default");

		JTextField textField_7 = new JTextField();
		thanhToan.add(textField_7, "7, 3, fill, default");
		textField_7.setColumns(10);
	}

	public JTable getTable() {
		return this.table;
	}

	public Vector<Vector<String>> getData() {
		return this.data;
	}

	public DefaultTableModel getTableModel() {
		return this.tableModel;
	}

	public Vector<String> getColumnNames() {
		return this.columnNames;
	}

	public void setValue(int sumMoney, double sumThue, int sum) {
		this.sumMoney.setText(sumMoney + "");
		this.sumThue.setText(sumThue + "");
		this.sum.setText(sum + "");
	}

}
